package org.geo.file.controller;

import org.geo.common.log.Log;
import org.geo.common.response.Responser;
import org.geo.common.response.model.JsonResponse;
import org.geo.file.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

/**
 * 文件上传Controller
 *
 * @author ljy
 */
@RestController
@RequestMapping("/file")
public class FileUploadController {
    @Autowired
    private FileUploadService fileUploadService;

    /**
     * 上传头像
     *
     * @param request HttpServletRequest
     * @return Response
     */
    @PutMapping("/avatar")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public JsonResponse uploadAvatar(@Context HttpServletRequest request) {
        try {
            String fileUrl = fileUploadService.uploadAvatar(request);
            return Responser.success(fileUrl);
        } catch (Exception e2) {
            Log.e(e2, "");
            return Responser.failed("系统异常");
        }
    }
}
